package fonkin.com.wifisignapp.acticity.aboutMe;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.bean.user.User;
import fonkin.com.wifisignapp.constants.AppConstants;
import fonkin.com.wifisignapp.retrofit.BaseRetrofit;
import fonkin.com.wifisignapp.utils.MD5Util;
import fonkin.com.wifisignapp.utils.ReturnVo;
import fonkin.com.wifisignapp.utils.ShowToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ResetPwd extends AppCompatActivity {


    EditText oldPwd,newPwd,confirmPwd;
    TextView errorOldPwd,errorConfirmPwd;
    SharedPreferences pref;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_pwd);
        initView();
    }

    private void initView() {
        pref = PreferenceManager.getDefaultSharedPreferences(ResetPwd.this);
        oldPwd = (EditText)findViewById(R.id.old_pwd);
        newPwd = (EditText)findViewById(R.id.new_pwd);
        confirmPwd = (EditText)findViewById(R.id.confirm_pwd);
        errorOldPwd = (TextView)findViewById(R.id.error_old_pwd);
        errorConfirmPwd = (TextView)findViewById(R.id.error_confirm_pwd);
    }

    public void updatePwd(View v){
        String oldPwdStr = oldPwd.getText().toString().trim();
        String newPwdStr = newPwd.getText().toString().trim();
        String confirmPwdStr = confirmPwd.getText().toString().trim();
        if ("".equals(oldPwdStr)){
            ShowToast.CenterToast("请输入旧密码",ResetPwd.this);
        }else if("".equals(newPwdStr)){
            ShowToast.CenterToast("请输入新密码",ResetPwd.this);
        }else if("".equals(confirmPwdStr)){
            ShowToast.CenterToast("请输入确定密码",ResetPwd.this);
        }else if (!MD5Util.convertMD5(pref.getString("user_pwd","")).equals(oldPwdStr)){
            errorOldPwd.setText("原密码输入有误！");
        }else if(!confirmPwdStr.equals(newPwdStr)){
            errorConfirmPwd.setText("两次输入的密码不相同！");
        }else{
                errorOldPwd.setText("");
                errorConfirmPwd.setText("");
                BaseRetrofit.GetBaseRetrofit().resetPwd(newPwdStr,pref.getString("user_id","")).enqueue(new Callback<ReturnVo<String>>() {
                    @Override
                    public void onResponse(Call<ReturnVo<String>> call, Response<ReturnVo<String>> response) {
                        if (response.isSuccessful()){
                            if (response.body().getCode().equals(1)) {
                                ShowToast.CenterPicToast("修改成功",ResetPwd.this,R.drawable.ic_check_circle_black_24dp );
                            }else{
                                ShowToast.CenterPicToast("修改失败",ResetPwd.this,R.drawable.ic_clear_black_24dp);
                            }
                        }else{
                            ShowToast.ShowToastShort(AppConstants.Fail_REQUEST,ResetPwd.this);
                        }
                    }

                    @Override
                    public void onFailure(Call<ReturnVo<String>> call, Throwable t) {
                        ShowToast.ShowToastShort(AppConstants.Fail_RESPONSE,ResetPwd.this);
                    }
                });
        }


    }
}
